//
//  UserManager.m
//  HuaWan
//
//  Created by HuaWan on 2022.
//  Copyright © 2022 HuaWan. All rights reserved.
//

#import "UserManager.h"

@implementation UserManager

+ (instancetype)shareInstance {
    static UserManager *_MCUserManager = nil;
    static dispatch_once_t oncePredicate;
    dispatch_once(&oncePredicate, ^{
        _MCUserManager = [[self alloc] init];
    });
    return _MCUserManager;
}

- (void)saveUserData:(UserManager *)userManager {
    NSDictionary *dic = userManager.mj_keyValues;
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:dic forKey:@"HuaWan"];
    [userDefaults synchronize];
}

- (UserManager *)userManager {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSDictionary *dic = [userDefaults objectForKey:@"HuaWan"];
    UserManager *userManager = [UserManager mj_objectWithKeyValues:dic];
    if (!userManager) {
        userManager = [UserManager new];
        userManager.uuid = [self getGuid];
    }
    if (kIsCeShi > 0) {
        userManager.isSubscribe = YES;
    }
    return userManager;
}

//获取随机guid guid一般是32位的，生成规则和随机数不同
- (NSString*)getGuid {
    CFUUIDRef uuid_ref = CFUUIDCreate(NULL);
    CFStringRef uuid_string_ref= CFUUIDCreateString(NULL, uuid_ref);
    CFRelease(uuid_ref);
    NSString *uuid = [NSString stringWithString:(__bridge NSString*)uuid_string_ref];
    CFRelease(uuid_string_ref);
    return [uuid stringByReplacingOccurrencesOfString:@"-" withString:@""];
}

@end
